package com.dai.zhenfujin.visitingcard.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ColleagueListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.visitingcard.app.adapter.ColleagueAdapter;
import com.dai.zhenfujin.visitingcard.app.bean.ColleagueBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
同事
 */
public class ColleagueActivity extends BaseActivity {

    private ListView listView;
    private TextView tvTitle;
    private ImageView iv_no_msg;
    private FrameLayout imageView;
    private ColleagueAdapter adapter;
    private List<ColleagueBean> beanList;
    private int page = 1;
    private int number = 10;
    private List<ColleagueListBean.UserListBean> user_list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colleague);
        tvTitle = findViewById(R.id.title_textview);
        tvTitle.setText("同事");
        imageView = findViewById(R.id.fl_img);
        listView = findViewById(R.id.colleague_lv);
        iv_no_msg = findViewById(R.id.iv_no_msg);
        String company_name =  getIntent().getStringExtra("company_name");
        if (TextUtils.isEmpty(company_name))
            return;
        initView();

        beanList = new ArrayList<>();
        for (int i = 0; i <20 ; i++) {
            beanList.add(new ColleagueBean("张艺兴","运营总监","科技有限公司","工作范围","100米"));
        }

        adapter = new ColleagueAdapter(this,user_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(ColleagueActivity.this,PersonalDetailsActivity.class);
            intent.putExtra("user_id", user_list.get(position).getUser_id());
            intent.putExtra("type", 2);
            startActivity(intent);
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("company_name", getIntent().getStringExtra("company_name"));
        params2.put("page", page + "");
        params2.put("number", number + "");
        params2.put("user_id", getIntent().getStringExtra("user_id"));
        params2.put("loc_x", SPUtils.getString(this, "longitude"));
        params2.put("loc_y", SPUtils.getString(this, "latitude"));
        params.put("req_bd",  params2);

        HttpClient.post(this, Constants.COLLEAGUELIST , params, new CallBack<ColleagueListBean>() {
            @Override
            public void onSuccess(ColleagueListBean resultBean) {
                user_list.clear();
                user_list.addAll(resultBean.getUser_list());
                adapter.notifyDataSetChanged();
                if (user_list.size() == 0){
                    iv_no_msg.setVisibility(View.VISIBLE);
                }else {
                    iv_no_msg.setVisibility(View.GONE);
                }
            }
        });
    }
}
